package com.zb.service.impl;

import com.zb.entity.Collection;
import com.zb.entity.Item;
import com.zb.exception.MyException;
import com.zb.repository.CollectionRepository;
import com.zb.service.CollectionService;
import com.zb.service.ItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 收藏服务实现类
 * @author HBY
 */
@Service
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private CollectionRepository collectionRepository;
    @Resource
    private ItemService itemService;


    /**
     * 根据收藏夹id查找收藏夹
     *
     * @param id
     * @return Collection
     */
    @Override
    public Collection findById(Long id) {

        if(Objects.isNull(id)||id<0){
            throw new MyException("非法的商品id："+id);
        }

        Collection collection = collectionRepository.findById(id).orElse(null);
        if (collection == null){
            throw new MyException("收藏未找到，商品id:"+id);
        }
        return collection;
    }

    /**
     * 根据用户id查找收藏
     * @param userId
     * @return collection
     */
    @Override
    public Collection findByUser(Long userId) {

        Collection collection = collectionRepository.findByUser_Id(userId);
        if (collection == null){
            throw new MyException("收藏未找到，userId:"+userId);
        }
        return collection;
    }

    /**
     * 根据商品id查找收藏
     * @param list   收藏列表
     * @param itemId
     * @return 购物车订单item，不存在返回null
     */

    private Item findItemByItemId(List<Item> list , long itemId ){
        for (Item item : list){
            if(item.getId().equals(itemId)){
                return item;
            }
        }
        return null;
    }

    /**
     * 检查商品是否被收藏
     * @param itemId
     * @param id
     * @return 是否被收藏
     */
    @Override
    public boolean hasItem(long itemId, long id){

        return findItemByItemId(findById(id).getItems() , itemId) != null;
    }

    /**
     * 获取某商品被收藏次数
     * @param itemId
     * @return
     */
    @Override
    public long getCollectionCount(Long itemId) {
        if(itemId<0){
            throw new MyException("不合法的用户id");
        }
        return collectionRepository.countByItemId(itemId);
    }

    /**
     * 添加商品
     * @param userId
     * @param itemId
     * @return
     */

    @Override
    public Collection addItem(Long userId, Long itemId) {

        Collection collection = findByUser(userId);
        if (hasItem(collection.getId(), itemId))
        {
            throw new MyException("商品已经在收藏中");
        }
        collection.getItems().add(itemService.findById(itemId));
        return collectionRepository.save(collection);
    }

    /**
     * 移除商品
     * @param collectionId
     * @param itemId
     * @return
     */
    @Override
    public Collection removeItem(Long collectionId, Long itemId) {

        Collection collection = findById(collectionId);
        List<Item> list = collection.getItems();
        Item item = findItemByItemId(list,itemId);
        if (item != null){
            list.remove(item);
        }else{
            throw new MyException("商品未被收藏");
        }
        return collectionRepository.save(collection);
    }





}
